export default {
    namespaced: true,
    state: () => ({
        activeRoute: {}, //当前active的tab
        tabsList: [], //tabs
    }),
    mutations: {
        SET_ACTIVE_ROUTE(state, item) {
            state.activeRoute = item
            sessionStorage.activeRoute = JSON.stringify(state.activeRoute)
        },
        ADD_TABS_LIST(state, route) {
            if (state.tabsList.findIndex(i => i.name == route.name) == -1) {
                let activeRouteIndex = state.tabsList.findIndex(i => i.name == state.activeRoute.name)
                let robj = {
                    key: route.name,
                    name: route.name,
                    title: route.title,
                    path: route.path
                }
                if (route.isDetail && activeRouteIndex > -1) {
                    state.tabsList.splice(activeRouteIndex + 1, 0, robj)
                } else {
                    state.tabsList.push(robj)
                }

                // 设置标签缓存最大上线
                let limitLen = 10
                if (state.tabsList.length > limitLen ) {
                    let cutLen = state.tabsList.length - limitLen
                    for(let i = cutLen; i >= 0 ; i--) {
                        if(state.tabsList[i].name != 'base') {
                            state.tabsList.splice(i, 1)
                        }
                    }
                }
            }
            sessionStorage.tabsList = JSON.stringify(state.tabsList)
        },
        RESET_TAB_PATH(state, { routeName, path }) {
            state.tabsList.forEach(item => {
                if( item.name == routeName ) {
                    item.path = path
                }
            })
            sessionStorage.tabsList = JSON.stringify(state.tabsList)
        },
        REMOVE_TABS_LIST(state, routeName) {
            let index = state.tabsList.findIndex(i => i.name == routeName)
            console.log(index, routeName, 'dadsfadsf');
            state.tabsList.splice(index, 1)
            console.log(JSON.parse(JSON.stringify(state.tabsList)));
            sessionStorage.tabsList = JSON.stringify(state.tabsList)
        },
        SET_TABS_LIST(state, tabsList) {
            state.tabsList = []
            state.tabsList = tabsList || []
            sessionStorage.tabsList = JSON.stringify(state.tabsList)
        },
        CLEAR_TABS_LIST(state) {
            state.tabsList = []
            sessionStorage.tabsList = ''
            sessionStorage.activeRoute = ''
        },
        CLEAR_TABS_ITEM_PATH_QUERY(state, routeName) {
            state.tabsList.forEach(item => {
                if(item.name == routeName) {
                    let index = item.path.indexOf('?')
                    console.log('index', index);
                    if(index > -1) {
                        console.log(item.path.slice(0, index));
                        item.path = item.path.slice(0, index)
                    }
                }
            })
            sessionStorage.tabsList = JSON.stringify(state.tabsList)
        }
    },
    actions: {
        routerPage({ commit, state }, tabInfo) {
            commit('ADD_TABS_LIST', tabInfo)
            commit('SET_ACTIVE_ROUTE', tabInfo)
        }
    }
}
